import { getData } from '@vimi/utils-api'

const state = {
  guardDatas: [],
}

const getters = {
  guardDatas: state => state.guardDatas,
}

const actions = {
  getGuardsData({ commit }, relatedId) {
    return new Promise((resolve, reject) => {
      if (!relatedId) {
        return resolve([])
      }
      getData(
        'credit',
        'gua_contract',
        { 'customer_related_id.eq': relatedId },
        {
          N1order_num: 'asc',
        }
      )
        .then(res => {
          const data = (res && res.data) || []
          commit('setGuards', data)
          resolve(data)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
}

const mutations = {
  setGuards: (state, payload) => (state.guardDatas = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
